package com.lemon.smartsafetynew.ui.activity.task;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.entity.tab.TabEntity;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafetynew.entity.User;
import com.lemon.smartsafetynew.entity.event.common.UserInfoRefreshEvent;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolBo;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolTaskItemVo;
import com.lemon.smartsafetynew.entity.task.InspectionIndexVo;
import com.lemon.smartsafetynew.entity.task.TaskBo;
import com.lemon.smartsafetynew.net.services.SafePatrolServices;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafetynew.ui.adapter.task.InspectionTaskAdapter;
import com.lemon.smartsafetynew.ui.fragment.task.InspectionTaskFragment;
import com.lemon.smartsafetynew.util.constants.KeyConstants;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by JiangAn on 2018/5/7.
 */

public class InspectionTaskActivity extends SwipeBackActivity {
    @Bind(R.id.tabLayout)
    CommonTabLayout tabLayout;
    @Bind(R.id.viewPager)
    ViewPager viewPager;
    private int mPosition;
    private InspectionTaskAdapter adapter;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private List<InspectionTaskFragment> mList = new ArrayList<>();
    private String[] mTitles = {"待巡查", "巡查中", "已巡查", "已过期"};

    @Override
    protected void initTitle() {
        this.setBarTitle(R.string.work_options_main_12);
    }

    @Override
    protected void callFinish() {
       /* tabLayout.showMsg(0, 100);
        tabLayout.setMsgMargin(0, -15, 5);*/
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_viewpage_my_tab;
    }

    private void initData() {
        InspectionTaskFragment waitInspection = InspectionTaskFragment.newInstance(TaskBo.TaskBoStatus.WAIT.getValue());
        InspectionTaskFragment duringInspection = InspectionTaskFragment.newInstance(TaskBo.TaskBoStatus.DURING.getValue());
        InspectionTaskFragment finishInspection = InspectionTaskFragment.newInstance(TaskBo.TaskBoStatus.FINISH.getValue());
        InspectionTaskFragment overDateInspection = InspectionTaskFragment.newInstance(TaskBo.TaskBoStatus.OVERDATE.getValue());
        mList.clear();
        mList.add(waitInspection);
        mList.add(duringInspection);
        mList.add(finishInspection);
        mList.add(overDateInspection);
    }

    @Override
    protected void initViews() {
        super.initViews();
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }
        initData();
        tabLayout.setTabData(mTabEntities);
        adapter = new InspectionTaskAdapter(getSupportFragmentManager(), mTitles, mList);
        viewPager.setAdapter(adapter);
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setCurrentItem(0);
        viewPager.setOffscreenPageLimit(2);
    }

    @Override
    protected void onResume() {
        super.onResume();
        LemonRequestParams params = new LemonRequestParams("");
        SafePatrolServices
                .init()
                .getInspectionIndex(params);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMainThread(HttpCallbackEvent event) {

        if (KeyConstants.URL_GET_INSPECTION_INDEX.equals(event.getTag())) {                             //获取任务个数
            if (event.getHttpFlow() == HandlerCallback.HttpFlow.START) {
            } else if (event.getHttpFlow() == HandlerCallback.HttpFlow.FINISH) {
                if (event.getEventResult() == Constants.Result.SUCCESS) {
                    if (event.getObj() instanceof InspectionIndexVo) {
                        int numPosition1 = ((InspectionIndexVo) event.getObj()).getNoExecuteCount();
                        int numPosition2 = ((InspectionIndexVo) event.getObj()).getIngCount();
                        if (numPosition1 > 0) {
                            tabLayout.showMsg(0, numPosition1);
                            tabLayout.setMsgMargin(0, -5, 10);
                        } else {
                            tabLayout.hideMsg(0);
                        }
                        if (numPosition2 > 0) {
                            tabLayout.showMsg(1, numPosition2);
                            tabLayout.setMsgMargin(1, -5, 10);
                        } else {
                            tabLayout.hideMsg(1);
                        }
                    }
                }
            }
        }
    }

}
